package com.guli.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.Result;
import com.guli.eduservice.entity.EduCourse;
import com.guli.eduservice.entity.vo.CourseInfoVo;
import com.guli.eduservice.entity.vo.CoursePublishVo;
import com.guli.eduservice.entity.vo.CourseQuery;
import com.guli.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @GetMapping
    public Result getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return Result.ok().data("list",list);
    }


    @GetMapping("pageCourse/{current}/{limit}")
    public Result getCourseList(@PathVariable Long current, @PathVariable Long limit) {
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        List<EduCourse> list = eduCourseService.list(null);
        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return Result.ok().data(map);
    }

    //条件查询
    @ApiOperation(value = "条件查询")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageCourseCondition(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String name = courseQuery.getName();
//        Integer level = courseQuery.getLevel();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
//        if (!StringUtils.isEmpty(level)) {
//            wrapper.eq("level", level);
//        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_create");
        eduCourseService.page(pageCourse, wrapper);
        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
        return Result.ok().data("total", total).data("rows", records);
    }


    //添加
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId", id);
    }


    //id查询
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.gerCouseIndo(courseId);
        return Result.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.PublishCourseInfo(id);
        return Result.ok().data("publishCourse", coursePublishVo);
    }

    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }


    //删除
    @DeleteMapping("{courseId}")
    public Result deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return Result.ok();
    }
}

